//  
//  BGOProfile.m
//  Bango iOS API
//
//  Created by William Shakour on September 8, 2011.
//  Copyrights © 2011 Bango - http://www.bango.net/ - All rights reserved.
//

#import "BGOProfile.h"
#import "BGOEvent.h"

#import "BGOFieldNameConstants.h"

@implementation BGOProfile
+ (BGOProfile *)profile {
  return [[[BGOProfile alloc] init] autorelease];
}
@dynamic events;
- (void)addToEvents:(BGOEvent *)event {
  if (_events == nil) {
    _events = [[NSMutableArray array] retain];
  }
  
  [_events addObject:event];
}
- (NSArray *)events {
  return _events;
}
@synthesize device=_device;
@synthesize applicationKey=_applicationKey;
@synthesize applicationChannel=_applicationChannel;
@synthesize applicationType=_applicationType;
@synthesize applicationVersion=_applicationVersion;
@synthesize ipAddress=_ipAddress;
@synthesize userId=_userId;
@synthesize bangoUserId=_bangoUserId;
@dynamic created;
- (NSDate *) created {
  return [NSDate dateWithTimeIntervalSince1970:[_created doubleValue] / 1000.0];
}
- (void) setCreated:(NSDate *)val_ue {
  if (_created != nil) {
    [_created release];
  }
  _created = [[NSNumber numberWithDouble:[val_ue timeIntervalSince1970] * 1000.0] retain];
}
@synthesize networkOperator=_networkOperator;
@synthesize operatorName=_operatorName;
@synthesize operatorCountryIso=_operatorCountryIso;
@synthesize operatorConnectionType=_operatorConnectionType;
@synthesize sim=_sim;
@synthesize brand=_brand;
@synthesize locationSource=_locationSource;
@dynamic locationLatitude;
- (double) locationLatitude {
  return [_locationLatitude doubleValue];
}
- (void) setLocationLatitude:(double)val_ue {
  if (_locationLatitude != nil) {
    [_locationLatitude release];
  }
  _locationLatitude = [[NSNumber numberWithDouble:val_ue] retain];
  
}
@dynamic locationLongitude;
- (double) locationLongitude {
  return [_locationLongitude doubleValue];
}
- (void) setLocationLongitude:(double)val_ue {
  if (_locationLongitude != nil) {
    [_locationLongitude release];
  }
  _locationLongitude = [[NSNumber numberWithDouble:val_ue] retain];
}
@dynamic locationAltitude;
- (double) locationAltitude {
  return [_locationAltitude doubleValue];
}
- (void) setLocationAltitude:(double)val_ue {
  if (_locationAltitude != nil) {
    [_locationAltitude release];
  }
  _locationAltitude = [[NSNumber numberWithDouble:val_ue] retain];
}
@dynamic locationAccuracy;
- (double) locationAccuracy {
  return [_locationAccuracy doubleValue];
}
- (void) setLocationAccuracy:(double)val_ue {
  if (_locationAccuracy != nil) {
    [_locationAccuracy release];
  }
  _locationAccuracy = [[NSNumber numberWithDouble:val_ue] retain];
}
@synthesize version=_version;
@synthesize sdkVersion=_sdkVersion;
@dynamic networkType;
- (float) networkType {
  return [_networkType floatValue];
}
- (void) setNetworkType:(float)val_ue {
  if (_networkType != nil) {
    [_networkType release];
  }
  _networkType = [[NSNumber numberWithFloat:val_ue] retain];
}
@synthesize model=_model;
@synthesize product=_product;
@synthesize connectionType=_connectionType;
@dynamic sdkType;
- (int) sdkType {
  return [_sdkType intValue];
}
- (void) setSdkType:(int)val_ue {
  if (_sdkType != nil) {
    [_sdkType release];
  }
  _sdkType = [[NSNumber numberWithInt:val_ue] retain];
}
@dynamic isRoaming;
- (BOOL) isIsRoaming {
  return [_isRoaming boolValue];
}
- (void) setIsRoaming:(BOOL)val_ue {
  if (_isRoaming != nil) {
    [_isRoaming release];
  }
  _isRoaming = [[NSNumber numberWithBool:val_ue] retain];
}
@dynamic sessionRestartTime;
- (int) sessionRestartTime {
  return [_sessionRestartTime intValue];
}
- (void) setSessionRestartTime:(int)val_ue {
  if (_sessionRestartTime != nil) {
    [_sessionRestartTime release];
  }
  _sessionRestartTime = [[NSNumber numberWithInt:val_ue] retain];
}
@dynamic isProfilePosted;
- (BOOL) isProfilePosted {
  return [_profilePosted boolValue];
}
- (void) setProfilePosted:(BOOL)val_ue {
  if (_profilePosted != nil) {
    [_profilePosted release];
  }
  _profilePosted = [[NSNumber numberWithBool:val_ue] retain];
}
@synthesize buildId=_buildId;
- (NSMutableDictionary *) toJson {
  NSMutableDictionary * jsonObject = [super toJson];
  if (_events != nil) {
    NSMutableArray *jsonArray = [NSMutableArray arrayWithCapacity:_events.count];
    for (BGOEvent *event in _events) {
      [jsonArray addObject:[event toJson]];
    }
    [jsonObject setObject:jsonArray forKey:BGO_POST_EVENTS];
  }
  if (_device != nil) {
    [jsonObject setObject:_device forKey:BGO_POST_DEVICEMAKE];
  }
  if (_applicationKey != nil) {
    [jsonObject setObject:_applicationKey forKey:BGO_POST_APPLICATIONID];
  }
  if (_applicationChannel != nil) {
    [jsonObject setObject:_applicationChannel forKey:BGO_POST_APPLICATIONCHANNEL];
  }
  if (_applicationType != nil) {
    [jsonObject setObject:_applicationType forKey:BGO_POST_APPLICATIONTYPE];
  }
  if (_applicationVersion != nil) {
    [jsonObject setObject:_applicationVersion forKey:BGO_POST_APPLICATIONVERSION];
  }
  if (_ipAddress != nil) {
    [jsonObject setObject:_ipAddress forKey:BGO_POST_IP];
  }
  if (_userId != nil) {
    [jsonObject setObject:_userId forKey:BGO_POST_USERID];
  }
    
    if (_bangoUserId != nil) {
        [jsonObject setObject:_bangoUserId forKey:BGO_POST_BANGOUSERID];
    }
//  if (_created != nil) {
//    [jsonObject setObject:_created forKey:@"created"];
//  }
//  if (_networkOperator != nil) {
//    [jsonObject setObject:_networkOperator forKey:@"networkOperator"];
//  }
  if (_operatorName != nil) {
    [jsonObject setObject:_operatorName forKey:BGO_POST_OPERATORNAME];
  }
  if (_operatorCountryIso != nil) {
    [jsonObject setObject:_operatorCountryIso forKey:BGO_POST_COUNTRYISO];
  }
  if (_operatorConnectionType != nil) {
    [jsonObject setObject:_operatorConnectionType forKey:BGO_POST_OPERATORCONNECTIONTYPE];
  }
//  if (_sim != nil) {
//    [jsonObject setObject:_sim forKey:@"sim"];
//  }
  if (_brand != nil) {
    [jsonObject setObject:_brand forKey:BGO_POST_DEVICEBRAND];
  }
//  if (_locationSource != nil) {
//    [jsonObject setObject:_locationSource forKey:@"LocationSource"];
//  }
  if (_locationLatitude != nil) {
    [jsonObject setObject:_locationLatitude forKey:BGO_POST_LOCATIONLATITUDE];
  }
  if (_locationLongitude != nil) {
    [jsonObject setObject:_locationLongitude forKey:BGO_POST_LOCATIONLONGITUDE];
  }
  if (_locationAltitude != nil) {
    [jsonObject setObject:_locationAltitude forKey:BGO_POST_LOCATIONALTITUDE];
  }
  if (_locationAccuracy != nil) {
    [jsonObject setObject:_locationAccuracy forKey:BGO_POST_LOCATIONACCURACY];
  }
  if (_version != nil) {
    [jsonObject setObject:_version forKey:BGO_POST_DEVICEVERSION];
  }
  if (_sdkVersion != nil) {
    [jsonObject setObject:_sdkVersion forKey:BGO_POST_SDKVERSION];
  }
//  if (_networkType != nil) {
//    [jsonObject setObject:_networkType forKey:@"networkType"];
//  }
  if (_model != nil) {
    [jsonObject setObject:_model forKey:BGO_POST_DEVICEMODEL];
  }
//  if (_product != nil) {
//    [jsonObject setObject:_product forKey:@"product"];
//  }
  if (_connectionType != nil) {
    [jsonObject setObject:_connectionType forKey:BGO_POST_CONNECTIONTYPE];
  }
  if (_sdkType != nil) {
    [jsonObject setObject:_sdkType forKey:BGO_POST_SDKTYPE];
  }
  if (_isRoaming != nil) {
    [jsonObject setObject:_isRoaming forKey:BGO_POST_ISROAMING];
  }
  if (_sessionRestartTime != nil) {
    [jsonObject setObject:_sessionRestartTime forKey:BGO_POST_SESSIONRESTARTTIME];
  }
//  if (_buildId != nil) {
//    [jsonObject setObject:_buildId forKey:@"buildId"];
//  }
  
  if (_profilePosted != nil) {
    [jsonObject setObject:_profilePosted forKey:@"profilePosted"];
  }
  
	bool canMakeCalls = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]];
	[jsonObject setObject:[NSNumber numberWithBool:canMakeCalls]
                        forKey:BGO_POST_DEVICEISMOBILE];
	
	[jsonObject setObject:[UIDevice currentDevice].systemName
                        forKey:BGO_POST_OPERATINGSYSTEM];
	[jsonObject setObject:[UIDevice currentDevice].systemVersion
                        forKey:BGO_POST_OPERATINGSYSTEMVERSION];
  
  return jsonObject;
}
- (void)fromJson:(NSDictionary *) jsonObject {
  [super fromJson:jsonObject];
  if ([jsonObject objectForKey:BGO_POST_EVENTS] != nil) {
    NSArray *jsonArray = [jsonObject objectForKey:BGO_POST_EVENTS];
    if (_events != nil) {
      [_events removeAllObjects];
    }
    BGOEvent *event = nil;
    for (NSDictionary *jsonArrayItem in jsonArray) {
      event = [BGOEvent event];
      [event fromJson:jsonArrayItem];
      [self addToEvents:event];
    }
  } else {
    [_events release];
    _events = nil;
  }
  if ([jsonObject objectForKey:BGO_POST_DEVICEMAKE] != nil) {
    self.device = [jsonObject objectForKey:BGO_POST_DEVICEMAKE];
  }
  if ([jsonObject objectForKey:BGO_POST_APPLICATIONID] != nil) {
    self.applicationKey = [jsonObject objectForKey:BGO_POST_APPLICATIONID];
  }
  if ([jsonObject objectForKey:BGO_POST_APPLICATIONCHANNEL] != nil) {
    self.applicationChannel = [jsonObject objectForKey:BGO_POST_APPLICATIONCHANNEL];
  }
  if ([jsonObject objectForKey:BGO_POST_APPLICATIONTYPE] != nil) {
    self.applicationType = [jsonObject objectForKey:BGO_POST_APPLICATIONTYPE];
  }
  if ([jsonObject objectForKey:BGO_POST_APPLICATIONVERSION] != nil) {
    self.applicationVersion = [jsonObject objectForKey:BGO_POST_APPLICATIONVERSION];
  }
  if ([jsonObject objectForKey:BGO_POST_IP] != nil) {
    self.ipAddress = [jsonObject objectForKey:BGO_POST_IP];
  }
  if ([jsonObject objectForKey:BGO_POST_USERID] != nil) {
    self.userId = [jsonObject objectForKey:BGO_POST_USERID];
  }
  if ([jsonObject objectForKey:@"created"] != nil) {
    _created = [[jsonObject objectForKey:@"created"] retain];
  }
  if ([jsonObject objectForKey:@"networkOperator"] != nil) {
    self.networkOperator = [jsonObject objectForKey:@"networkOperator"];
  }
  if ([jsonObject objectForKey:BGO_POST_OPERATORNAME] != nil) {
    self.operatorName = [jsonObject objectForKey:BGO_POST_OPERATORNAME];
  }
  if ([jsonObject objectForKey:BGO_POST_COUNTRYISO] != nil) {
    self.operatorCountryIso = [jsonObject objectForKey:BGO_POST_COUNTRYISO];
  }
  if ([jsonObject objectForKey:BGO_POST_OPERATORCONNECTIONTYPE] != nil) {
    _operatorConnectionType = [[jsonObject objectForKey:BGO_POST_OPERATORCONNECTIONTYPE] retain];
  }
  if ([jsonObject objectForKey:@"sim"] != nil) {
    self.sim = [jsonObject objectForKey:@"sim"];
  }
  if ([jsonObject objectForKey:BGO_POST_DEVICEBRAND] != nil) {
    self.brand = [jsonObject objectForKey:BGO_POST_DEVICEBRAND];
  }
  if ([jsonObject objectForKey:BGO_POST_LOCATIONSOURCE] != nil) {
    self.locationSource = [jsonObject objectForKey:BGO_POST_LOCATIONSOURCE];
  }
  if ([jsonObject objectForKey:BGO_POST_LOCATIONLATITUDE] != nil) {
    _locationLatitude = [[jsonObject objectForKey:BGO_POST_LOCATIONLATITUDE] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_LOCATIONLONGITUDE] != nil) {
    _locationLongitude = [[jsonObject objectForKey:BGO_POST_LOCATIONLONGITUDE] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_LOCATIONALTITUDE] != nil) {
    _locationAltitude = [[jsonObject objectForKey:BGO_POST_LOCATIONALTITUDE] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_LOCATIONACCURACY] != nil) {
    _locationAccuracy = [[jsonObject objectForKey:BGO_POST_LOCATIONACCURACY] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_DEVICEVERSION] != nil) {
    self.version = [jsonObject objectForKey:BGO_POST_DEVICEVERSION];
  }
  if ([jsonObject objectForKey:BGO_POST_SDKVERSION] != nil) {
    _sdkVersion = [[jsonObject objectForKey:BGO_POST_SDKVERSION] retain];
  }
  if ([jsonObject objectForKey:@"networkType"] != nil) {
    _networkType = [[jsonObject objectForKey:@"networkType"] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_DEVICEMODEL] != nil) {
    self.model = [jsonObject objectForKey:BGO_POST_DEVICEMODEL];
  }
  if ([jsonObject objectForKey:@"product"] != nil) {
    self.product = [jsonObject objectForKey:@"product"];
  }
  if ([jsonObject objectForKey:BGO_POST_CONNECTIONTYPE] != nil) {
    self.connectionType = [jsonObject objectForKey:BGO_POST_CONNECTIONTYPE];
  }
  if ([jsonObject objectForKey:BGO_POST_SDKTYPE] != nil) {
    _sdkType = [[jsonObject objectForKey:BGO_POST_SDKTYPE] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_ISROAMING] != nil) {
    _isRoaming = [[jsonObject objectForKey:BGO_POST_ISROAMING] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_SESSIONRESTARTTIME] != nil) {
    _sessionRestartTime = [[jsonObject objectForKey:BGO_POST_SESSIONRESTARTTIME] retain];
  }
  if ([jsonObject objectForKey:@"buildId"] != nil) {
    self.buildId = [jsonObject objectForKey:@"buildId"];
  }
  if ([jsonObject objectForKey:@"profilePosted"] != nil) {
    _profilePosted = [[jsonObject objectForKey:@"profilePosted"] retain];
  }
}
- (void) dealloc {
  [_events release];
  [_device release];
  [_applicationKey release];
  [_applicationChannel release];
  [_applicationType release];
  [_applicationVersion release];
  [_ipAddress release];
  [_userId release];
  [_created release];
  [_networkOperator release];
  [_operatorName release];
  [_operatorCountryIso release];
  [_operatorConnectionType release];
  [_sim release];
  [_brand release];
  [_locationSource release];
  [_locationLatitude release];
  [_locationLongitude release];
  [_locationAltitude release];
  [_locationAccuracy release];
  [_version release];
  [_sdkVersion release];
  [_networkType release];
  [_model release];
  [_product release];
  [_connectionType release];
  [_sdkType release];
  [_isRoaming release];
  [_sessionRestartTime release];
  [_buildId release];
  [_profilePosted release];
  [super dealloc];
}
@end